package com.jinyu.jyly.ui.login;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.LoginInfo;
import com.jinyu.jyly.global.GlobalConstants;
import com.jinyu.jyly.ui.MainActivity;
import com.jinyu.jyly.utils.AES;
import com.jinyu.jyly.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 登录
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {


    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.tv_go_register)
    TextView tvGoRegister;
    @BindView(R.id.tv_forget_psw)
    TextView tvForgetPsw;
    private LoginPresenter presenter;
    private String deviceId;

    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter getPresenter() {
        if (presenter == null) {
            presenter = new LoginPresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
//        deviceId = GetDeviceId.getDeviceId(this);
//        Log.e("","_"+ deviceId);
    }


    @OnClick({R.id.btn_sub, R.id.tv_go_register, R.id.tv_forget_psw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_sub:
                String mobile = etMobile.getText().toString().trim();
                String psw = etPsw.getText().toString().trim();
                if (TextUtils.isEmpty(mobile)) {
                    showToast("手机号不能为空");
                    return;
                }

                if (TextUtils.isEmpty(psw)) {
                    showToast("密码不能为空");
                    return;
                }

                boolean showing = mTipDialog.isShowing();
                if(!showing){
                    try {
                        //登录
                        mTipDialog.show();
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("username", mobile);
                        jsonObject.put("password", psw);
//                        jsonObject.put("code", deviceId);
                        String pamam = AES.encryptToHexString(jsonObject.toString());
                        presenter.onLogin(pamam);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.tv_go_register:
                Intent mgoRegisterIntent = new Intent(this, RegisterActivity.class);
                startActivity(mgoRegisterIntent);
                break;
            case R.id.tv_forget_psw:
                Intent mForgetIntent = new Intent(this, ForgetPswActivity.class);
                startActivity(mForgetIntent);
                break;
        }
    }

    @Override
    public void onSendSms(AllInfoBean infoBean) {
        //空实现
    }

    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
    }

    @Override
    public void onRegister(AllInfoBean info) {
        //注册
    }

    @Override
    public void onForgetPaw(AllInfoBean info) {
        //空实现
    }

    @Override
    public void onLogin(AllInfoBean info) {
        mTipDialog.dismiss();
        switch (info.getStatus()) {
            case 200:
                try {
                    String data = info.getData();
                    String result = AES.decrypt(data);
                    Gson gson = new Gson();
                    LoginInfo loginInfo = gson.fromJson(result, LoginInfo.class);
                    if(loginInfo != null){
                        String token = loginInfo.getToken();
                        SharedPreferencesUtils.setParam(this, GlobalConstants.LOGIN_TOKEN,token);

                        Intent mMainIntent = new Intent(this, MainActivity.class);
                        startActivity(mMainIntent);
                        finish();
                    }
                    Log.e("tag",result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(info.getMsg());
                break;
            default:
                break;
        }
    }
}
